package bb.lanxing.devices.sync;

import android.text.TextUtils;

import com.garmin.fit.Mesg;
import com.garmin.fit.SessionMesg;

import bb.lanxing.activity.deeplink.DeepLinkPathConstants;
import bb.lanxing.devices.utils.XZDeviceHelper;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceSyncListener;
import bb.lanxing.lib.devices.core.sync.FitDeviceFile;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.igpsport.IGPSportDevice;
import bb.lanxing.lib.devices.sync.FitTrans;
import bb.lanxing.util.DateUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class IGSPortSyncManager extends FitSyncManager implements IGPSportDevice.IIGSPListener {
    private OkHttpClient client;
    private IGPSportDevice mIGSDevice;
    private IgpsManager mIgpsManager;

    @Override
    public boolean shouldSyncWithServer() {
        return true;
    }

    @Override
    public void abort() {
        super.abort();
    }

    @Override
    public void abortAll() {
        super.abortAll();
    }

    @Override
    public void delete(long j) {
        super.delete(j);
    }

    @Override
    public void deleteAll() {
        super.deleteAll();
    }

    @Override
    public DeviceFile getCurrentItem() {
        return super.getCurrentItem();
    }

    @Override
    public DeviceFile getItem(long j) {
        return super.getItem(j);
    }

    @Override
    public String getPath(long j) {
        return super.getPath(j);
    }

    @Override
    public int getSyncState(long j) {
        return super.getSyncState(j);
    }

    @Override
    public Object getTag() {
        return super.getTag();
    }

    @Override
    public boolean isSynchronised(long j) {
        return super.isSynchronised(j);
    }

    @Override
    public boolean isSynchronising() {
        return super.isSynchronising();
    }

    @Override
    public boolean isSynchronisingWithMultiFiles() {
        return super.isSynchronisingWithMultiFiles();
    }

    @Override
    public void onError(FitTrans fitTrans, Throwable th) {
        super.onError(fitTrans, th);
    }

    @Override
    public void onFitReceived(FitTrans fitTrans) {
        super.onFitReceived(fitTrans);
    }

    @Override
    public void onMesg(FitTrans fitTrans, Mesg mesg) {
        super.onMesg(fitTrans, mesg);
    }

    @Override
    public void onSport(FitTrans fitTrans, SessionMesg sessionMesg) {
        super.onSport(fitTrans, sessionMesg);
    }

    @Override
    public void readFileList() {
        super.readFileList();
    }

    @Override
    public void registerSyncListener(DeviceSyncListener deviceSyncListener) {
        super.registerSyncListener(deviceSyncListener);
    }

    @Override
    public void setCurrentFile(DeviceFile deviceFile) {
        super.setCurrentFile(deviceFile);
    }

    @Override
    public void setTag(Object obj) {
        super.setTag(obj);
    }

    @Override
    public boolean sync() {
        return super.sync();
    }

    @Override
    public boolean sync(long j) {
        return super.sync(j);
    }

    @Override
    public boolean sync(DeviceFile deviceFile) {
        return super.sync(deviceFile);
    }

    @Override
    public boolean sync(String str) {
        return super.sync(str);
    }

    @Override
    public void unregisterSyncListener(DeviceSyncListener deviceSyncListener) {
        super.unregisterSyncListener(deviceSyncListener);
    }

    public IGSPortSyncManager() {
        super(new DefaultFitProcessor(1), DeviceHelper.getWorkDir(11));
        IGPSportDevice iGPSportDevice = (IGPSportDevice) XZDeviceHelper.getDeviceManager().getDevice(1, 11);
        this.mIGSDevice = iGPSportDevice;
        if (iGPSportDevice != null) {
            iGPSportDevice.registerIGSListener(this);
        }
    }

    @Override
    void doReadFileList() {
        IGPSportDevice iGPSportDevice = this.mIGSDevice;
        if (iGPSportDevice != null) {
            iGPSportDevice.readFileList();
        }
    }

    @Override
    boolean doSync(DeviceFile deviceFile) {
        IGPSportDevice iGPSportDevice = this.mIGSDevice;
        if (iGPSportDevice == null || !iGPSportDevice.isServicesDiscovered()) {
            return false;
        }
        this.mIGSDevice.readFitFile(deviceFile.getId());
        return true;
    }

    @Override
    public boolean resync(DeviceFile deviceFile) {
        return sync(deviceFile);
    }

    @Override
    protected boolean doUpload(DeviceFile deviceFile) {
        if (this.mIgpsManager == null) {
            this.mIgpsManager = new IgpsManager();
        }
        String path = getPath(deviceFile.getId());
        if (!TextUtils.isEmpty(this.mIgpsManager.getToken()) && !TextUtils.isEmpty(path)) {
            if (this.client == null) {
                this.client = new OkHttpClient.Builder().build();
            }
            File file = new File(path);
            try {
                return this.client.newCall(new Request.Builder()
                                .url(Igps.API_UPLOAD_FIT)
                                .post(new MultipartBody.Builder()
                                        .setType(MultipartBody.FORM)
                                        .addFormDataPart(DeepLinkPathConstants.SCHEME_FILE, file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file))
                                        .addFormDataPart("memberid", String.valueOf(this.mIgpsManager.getMemberID()))
                                        .addFormDataPart("appid", Igps.APPID)
                                        .addFormDataPart("token"/*AssistPushConsts.MSG_TYPE_TOKEN*/, this.mIgpsManager.getToken())
                                        .build())
                                .build())
                        .execute()
                        .isSuccessful();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public void onReadFiles(List<IGPSportDevice.IGSPFile> list) {
        if (list != null) {
            List<DeviceFile> arrayList = new ArrayList<>();
            for (IGPSportDevice.IGSPFile iGSPFile : list) {
                if (iGSPFile.size >= 1000.0f) {
                    FitDeviceFile fitDeviceFile = new FitDeviceFile();
                    fitDeviceFile.id = iGSPFile.id;
                    fitDeviceFile.startTime = iGSPFile.id;
                    fitDeviceFile.timestamp = iGSPFile.timestamp;
                    fitDeviceFile.localTimestamp = iGSPFile.localTimeStamp;
                    fitDeviceFile.size = (long) iGSPFile.size;
                    fitDeviceFile.name = DateUtil.format0.format(iGSPFile.id);
                    fitDeviceFile.path = getPath(iGSPFile.id);
                    arrayList.add(fitDeviceFile);
                }
            }
            notifyReadFileList(arrayList);
            return;
        }
        notifyReadFileList(null);
    }

    @Override
    public void release() {
        if (this.client != null) {
            this.client = null;
        }
        this.mIgpsManager = null;
        IGPSportDevice iGPSportDevice = this.mIGSDevice;
        if (iGPSportDevice != null) {
            iGPSportDevice.removeIGSListener(this);
        }
        this.mIGSDevice = null;
        super.release();
    }

    @Override
    public void onReceivedData(int i, int i2, byte[] bArr) {
        onReceivedFitData(bArr, bArr == null);
    }

    @Override
    public void onProgressUpdate(int i) {
        DeviceFile currentItem = getCurrentItem();
        if (currentItem != null) {
            notifyProgressUpdate(currentItem.getId(), i);
        }
    }
}